package com.payment.ktb.activity.main1;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.ListView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.payment.ktb.Interface.HttpCallbackListener;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.adapter.AdapterMessageNotice;
import com.payment.ktb.constants.HttpActions;
import com.payment.ktb.constants.HttpUrls;
import com.payment.ktb.model.MessageNoticeEntity;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.HttpUtils;
import com.payment.ktb.utils.LogUtil;
import com.payment.ktb.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by admin on 2017/9/22.
 */

public class MessageNoticeActivity extends BaseActivity {


    @BindView(R.id.prlv_messagenotice)
    PullToRefreshListView prlv_messagenotice;

    AdapterMessageNotice adapter;
    List<MessageNoticeEntity> listDatas;
    int page = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_messagenotice);
        ButterKnife.bind(this);
        initTitle("消息通知");
        prlv_messagenotice.setMode(PullToRefreshBase.Mode.BOTH);
        prlv_messagenotice.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                // 设置下拉刷新加载文本
                prlv_messagenotice.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新...");
                prlv_messagenotice.getLoadingLayoutProxy(true, false).setReleaseLabel("放开刷新...");
                prlv_messagenotice.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在加载...");
                //下拉刷新
                pullDownToRefresh();
            }
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                // 设置上拉加载文本
                prlv_messagenotice.getLoadingLayoutProxy(false, true).setPullLabel("上拉刷新...");
                prlv_messagenotice.getLoadingLayoutProxy(false, true).setReleaseLabel("放开刷新...");
                prlv_messagenotice.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载...");
                pullUptoLoad();


            }
        });
        initData();
    }

    private void initData() {

        if (!TextUtils.isEmpty(getIntent().getStringExtra("records"))) {
            //data
            listDatas = new Gson().fromJson(getIntent().getStringExtra("records"), new TypeToken<List<MessageNoticeEntity>>() {}.getType());

            //adapter
            adapter = new AdapterMessageNotice(mContext,listDatas);

            //bind
            prlv_messagenotice.setAdapter(adapter);
        }


    }




    //下拉刷新
        public void pullDownToRefresh() {

            progressDialogUtils.showProgress();
            page = 0;
            //构造传输参数
            Map<String, String> map = new HashMap<String,String>();
            //1.系统消息，2活动消息
            map.put("noticeType", "1");
            map.put("page", String.valueOf(page));

            //向服务器发送请求
            HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserNoticeMsgListState, map, mQueue, new HttpCallbackListener() {
                @Override
                public void onFinish(String response) {
                    progressDialogUtils.dismissProgress();
                    prlv_messagenotice.onRefreshComplete();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (!TextUtils.isEmpty(jsonObject.optString("records").toString())) {
                            //data
                            listDatas = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<MessageNoticeEntity>>() {}.getType());
                            //adapter
                            adapter = new AdapterMessageNotice(mContext,listDatas);
                            //bind
                            prlv_messagenotice.setAdapter(adapter);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                    }
                }

                @Override
                public void onError(VolleyError volleyError) {
                    progressDialogUtils.dismissProgress();
                    prlv_messagenotice.onRefreshComplete();
                    AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
                }
            });
        }




    //上拉加载更多
        public void pullUptoLoad() {

            progressDialogUtils.showProgress();
            page = page + 1;
            //构造传输参数
            Map<String, String> map = new HashMap<String,String>();
            //1.系统消息，2活动消息
            map.put("noticeType", "1");
            map.put("page", String.valueOf(page));

            //向服务器发送请求
            HttpUtils.HttpRequestByVollery(HttpUrls.URL, HttpActions.UserNoticeMsgListState, map, mQueue, new HttpCallbackListener() {
                @Override
                public void onFinish(String response) {
                    progressDialogUtils.dismissProgress();
                    prlv_messagenotice.onRefreshComplete();
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        //data
                        if (jsonObject.has("records")){
                            List<MessageNoticeEntity> temp = new Gson().fromJson(jsonObject.getJSONArray("records").toString(), new TypeToken<List<MessageNoticeEntity>>() {}.getType());
                            listDatas.addAll(temp);
                            adapter.notifyDataSetChanged();
                        }else {
                            ToastUtils.showToast("没有更多数据啦！");
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                        AlertDialogUtils.showAlertDialog(mContext, getResources().getString(R.string.json_parse_error));
                    }
                }

                @Override
                public void onError(VolleyError volleyError) {
                    progressDialogUtils.dismissProgress();
                    prlv_messagenotice.onRefreshComplete();
                    AlertDialogUtils.showAlertDialog(mContext, volleyError.getMessage());
                }
            });
        }




}
